package ioana.pack;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;

public class ListaOraseActivity extends ListActivity implements OnClickListener 
{
ArrayList<String> orasele;
OraseAdapter adapter;
Button buton;
String FILENAME = "orase.txt";
FileOutputStream fos;
String s;
    /** Called when the activity is first created. */
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	System.out.println("oncreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        orasele=new ArrayList<String>();
        try {
			BufferedReader fis= new BufferedReader(new InputStreamReader(openFileInput(FILENAME))); 
			do
			{
				s=fis.readLine();
				if (s!=null)
				{
					orasele.add(s);
					System.out.println(s);
				}
			}
			while (s!=null);
			fis.close();
			
        
		} catch (IOException e) {
			// TODO Auto-generated catch block
		}
 
        buton=(Button)findViewById(R.id.button);
        buton.setOnClickListener(this);
        adapter=new OraseAdapter(this,orasele);
        setListAdapter(adapter);
        getListView().setOnItemLongClickListener(new OnItemLongClickListener() 
        {
			public boolean onItemLongClick(AdapterView<?> listAdapter, View view, int position, long id) 
			{
				// TODO Auto-generated method stub
				adapter.stergeOras(position);
				return true;
			}
		});
    }
    public void onClick(View view) 
    {
		// TODO Auto-generated method stub
    	Intent intent = new Intent (this, AdaugaOras.class);
		startActivityForResult(intent, 100); 
    }  
    
    public void onActivityResult (int requestCode, int responseCode, Intent data)
    {
    	if (requestCode == 100)
    	{
    		if (responseCode==RESULT_OK)
    		{
    			String oras = data.getStringExtra("oras"); 
    			adapter.adaugaOras(oras);
    		}
    	}
    }
    @Override
    public void onListItemClick (ListView list, View v, int position, long id)
    {
    	Intent intent = new Intent (this, Prognoza.class);
    	intent.putExtra("nume oras", orasele.get(position));

		startActivity(intent);
    }
    @Override
    public void onPause() 
    {
    	super.onPause();
    	 try 
    	 {
			PrintWriter fos= new PrintWriter (openFileOutput(FILENAME, ListaOraseActivity.MODE_PRIVATE));
			for(int i=0; i<=orasele.size()-1; i=i+1)
			{
				fos.println(orasele.get(i));
			}
			  fos.close();
		} 
    	 catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    }
 
   
	}
    
